package com.mykolaBlyk.graphexample;


public class Equation   {
	private double a,b,c;
	
	public double getA(){
		return this.a;
	}
	
	public double getB(){
		return this.b;
	}
	
	public double getC(){
		return this.c;
	}
	
	public void setA(double a){
		this.a=a;
	}
	
	public void setB(double b){
		this.b=b;
	}
	
	public void setC(double c){
		this.c=c;
	}
	
	public Equation(double a, double b, double c){
		this.a=a;
		this.b=b;
		this.c=c;
	}
	
	public double calculateDiscriminant(){
		double d=b*b -4*a*c;
		return d;
	}
	
	private MyPoint calculateControlPointForCubic(MyPoint C, MyPoint P){
		double x=StaticFields.FIRST*C.getX() +StaticFields.SECOND*P.getX();
		double y=StaticFields.FIRST*C.getY() +StaticFields.SECOND*P.getY();
		return new MyPoint(x,y);		
	}
	
	private MyPoint calculateControlPointForQuadratic(double x1, double x2){
		double xp= (x2-x1)/2;
		double y=calculateFunction(x1)+calculateDerivative(x1)*xp;
		return new MyPoint((x1+x2)/2 , y);
	}
	
	public double calculateFunction(double x){
		return a*x*x +b*x+c;
	}
	 
	public double calculateDerivative(double x){
		return 2*a*x+b;
	}
	
	private MyPoint calculateLeftEdge(double x){
		double xp=x-5.0;
		return new MyPoint(xp, calculateFunction(xp));
	}
	
	private MyPoint calculateRigtEdge(double x){
		double xp=x+5.0;
		return new MyPoint(xp, calculateFunction(xp));
	}
	
	public double calculateFirstRoot(double d){
		double x=(-b +Math.sqrt(d))/(2*a);
		return x;
	}

	public double calculateSecondRoot(double d){
		double x=(-b-Math.sqrt(d))/(2*a);
		return x;
	}
	
	public MyPoint calculateExt(){
		double x=(-this.b)/(2*a);
		double y=this.calculateFunction(x);
		return new MyPoint(x,y);
	}
	
	public Points calculateGraph(){
		Points points=new Points();
		MyPoint ext=this.calculateExt();
		points.setExt(ext);

		double d=calculateDiscriminant();
		if(d>0){
			double firstRoot=calculateFirstRoot(d);
			double secondRoot=calculateSecondRoot(d);
		
			if(firstRoot>secondRoot){
				double temp=firstRoot;
				firstRoot=secondRoot;
				secondRoot=temp;
			}
		
			MyPoint leftRoot=new MyPoint(firstRoot,0);
			points.setLeftRoot(leftRoot);
			MyPoint rightRoot=new MyPoint(secondRoot, 0);
			points.setRightRoot(rightRoot);
		
			MyPoint leftEdge=calculateLeftEdge(firstRoot);
			points.setStart(leftEdge);
			MyPoint rightEdge=calculateRigtEdge(secondRoot);
			points.setEnd(rightEdge);
			
			MyPoint C1=calculateControlPointForQuadratic(leftEdge.getX(),rightEdge.getX());
			points.setC1(C1);
			
			MyPoint C2=calculateControlPointForQuadratic(firstRoot, secondRoot);
			points.setC2(C2);
			return points;
		}
		
		//not have crossing the line 
		//TO DO
		if(d==0){
			
		}
		//not have crossing the line 
		//TO DO
		if(d<0){
			
		}
		
		return null;
	}

}

